package com.wsp.service.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import com.wsp.apimanagement.common.impl.AbstratCdnUtilImpl;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.IOException;

/**
 * Package: com.wsp.apimanagement.common
 * Description： TODO
 *
 * @Author: 巫思平
 * Copyright: Copyright (c) 2020
 * Version: 1
 */
@Slf4j
@Component
@ConfigurationProperties("wsp.cdn.qiniu")
@Setter
public class QiNiuCdnUtil extends AbstratCdnUtilImpl {
    //七牛云配置
    private String path;
    private String accessKey;
    private String secretKey;
    private String bucket;

    //华南机房
    private static final Configuration CONFIGURATION = new Configuration(Region.region2());

    @Override
    public String uploadCommonFile(String fileName, BufferedInputStream inputStream) {
        //生成上传凭证
        Auth auth = Auth.create(accessKey, secretKey);
        String token = auth.uploadToken(bucket);
        //上传
       return doUploadCommonFile(fileName,inputStream,token);
    }

    private String doUploadCommonFile(String fileName, BufferedInputStream inputStream, String token) {
        //线程不安全对象==》局部变量
        UploadManager uploadManager = new UploadManager(CONFIGURATION);
        String newFileName = fileName+fileName();
        if(token==null) {
            token = getToken(newFileName);
        }
        //上传
        try {
            Response response = uploadManager.put(inputStream,newFileName,token,null,null);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(),DefaultPutRet.class);
            //返回七牛云存储路径
            return path + '/' + putRet.key;
        } catch (QiniuException e) {
            log.error(e.response.toString());
            e.printStackTrace();
        } finally {
            try {
                if(inputStream!=null){
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 七牛云自带的分片上传
     */
    @Override
    public String uploadBigCommonFile(String fileName, BufferedInputStream inputStream) {
        return doUploadCommonFile(fileName,inputStream,null);
    }

    @Override
    public<T> String getToken(T args) {
        if(!(args instanceof String)) {
            return null;
        }
        //设置转码操作参数
        String fops = "avthumb/mp4/vcodec/libx264";
        //设置转码的队列
        String pipeline = "default.sys";
        //可以对转码后的文件进行使用saveas参数自定义命名，当然也可以不指定文件会默认命名并保存在当前空间。
        String urlbase64 = UrlSafeBase64.encodeToString(bucket + ":" + args);
        String pfops = fops + "|saveas/" + urlbase64;
        //生成上传凭证
        Auth auth = Auth.create(accessKey, secretKey);
        return auth.uploadToken(bucket, null, 3600, new StringMap().putNotEmpty("persistentOps", pfops).putNotEmpty("persistentPipeline", pipeline));
    }

    @Override
    public String toString() {
        return path+" "+accessKey+" "+secretKey+" "+bucket;
    }
}
